local hx__xudai = fk.CreateSkill {
  name = "hx__xudai",
  tags = { Skill.Compulsory },
}

hx__xudai:addEffect(fk.BeforeCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if  player:hasSkill(hx__xudai.name) then
      for _, move in ipairs(data) do
        if move.skillName == "phase_draw" and move.to == player then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.skillName == "phase_draw" and move.to == player then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).sub_type ~= Card.SubtypeWeapon and Fk:getCardById(info.cardId).sub_type ~= Card.SubtypeArmor then
            room:addCardMark(Fk:getCardById(info.cardId), "hx__xudai-inhand")
          end
        end
      end
    end
  end,
})

hx__xudai:addEffect(fk.RoundEnd, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__xudai.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local kingdoms = {}
    for _, p in ipairs(room.alive_players) do
      table.insertIfNeed(kingdoms, p.kingdom)
    end
    local num = math.max(1, math.floor(#kingdoms / 2))
    room:damage({
      to = player,
      damage = num,
      skillName = hx__xudai.name,
    })
  end
})

hx__xudai:addEffect("atkrange", {
  correct_func = function(self, from, to)
    if from:hasSkill(hx__xudai.name) then
      return 1
    end
  end,
})

hx__xudai:addEffect("filter", {
  card_filter = function(self, card, player, isJudgeEvent)
    if player:hasSkill(hx__xudai.name) then
      return ((card.sub_type == Card.SubtypeWeapon or card.sub_type == Card.SubtypeArmor) or Fk:getCardById(card.id):getMark("hx__xudai-inhand") > 0) and
      (table.contains(player.player_cards[Player.Hand], card.id) or isJudgeEvent)
    end
  end,
  view_as = function(self, plain, card)
    if card.sub_type == Card.SubtypeWeapon or card.sub_type == Card.SubtypeArmor then
      local c = Fk:cloneCard("analeptic", card.suit, card.number)
      c.skillName = hx__xudai.name
      return c
    end
    if Fk:getCardById(card.id):getMark("hx__xudai-inhand") > 0 then
      local c = Fk:cloneCard("ex_nihilo", card.suit, card.number)
      c.skillName = hx__xudai.name
      return c
    end
  end,
})

hx__xudai:addEffect("targetmod", {
  residue_func = function(self, player, skill, scope, card)
    if skill and player:hasSkill(hx__xudai.name) then
      if skill.trueName == "analeptic_skill" then
        return 1
      end
    end
    return 0
  end,
})

hx__xudai:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__xudai.name) and data.card.trueName == "slash"
    and (data.to:isFemale() or data.to:isMale())
  end,
  on_use = function(self, event, target, player, data)
    if data.to:isFemale() then
      data:setResponseTimes(2)
    end
    if data.to:isMale() then
      if #player.room:askToDiscard(data.to, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = hx__xudai.name,
        prompt = "#hx__xudai-discard:::"..data.card:toLogString(),
        cancelable = true,
      }) == 0 then
        data.disresponsive = true
      end
    end
  end,
})

return hx__xudai
